package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.ztgf.shopping.models.bo.shop.ShopBlacklistBO;
import com.ztgf.shopping.models.dto.audit.AuditDTO;
import com.ztgf.shopping.models.dto.shop.*;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.audit.IAuditService;
import com.ztgf.shopping.models.service.shop.IShopInfoAuditService;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.vo.shop.ShopBlacklistVO;
import com.ztgf.shopping.models.vo.shop.ShopInfoVO;
import com.ztgf.user.models.dto.UserInfoDTO;
import com.ztgf.user.models.dto.UserLoginDTO;
import com.ztgf.user.models.service.IUserInfoService;
import com.ztgf.user.models.service.IUserLoginService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.shop.ShopBlacklistMapper;
import com.ztgf.shopping.models.service.shop.IShopBlacklistService;
import com.ztgf.shopping.models.bo.shop.ShopBlacklistSaveBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;

import java.util.ArrayList;
import java.util.List;

/**
 * 黑名单控制器
 *
 * @author zhanghualang
 * @Date 2020-06-16 16:01:00
 */
@DubboService
@Lazy
public class ShopBlacklistServiceImpl extends ServiceImpl<ShopBlacklistMapper, ShopBlacklistDTO> implements IShopBlacklistService{
    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    @Lazy
    private ShopInfoServiceImpl iShopInfoService;
    @DubboReference
    private IUserLoginService iUserLoginService;
    @Autowired
    private ShopInfoAuditServiceImpl shopInfoAuditService;
    @Autowired
    private ShopQualificationAuditServiceImpl shopQualificationAuditService;
    @Autowired
    private ShopCategoryRelationAuditServiceImpl shopCategoryRelationAuditService;
    @Autowired
    private ShopBrandRelationAuditServiceImpl shopBrandRelationAuditService;
    @DubboReference
    private IAuditService iAuditService;
    @DubboReference
    private IUserInfoService iUserInfoService;

    @Override
    public boolean add(ShopBlacklistSaveBO saveBO) {
        ShopBlacklistDTO dto = new ShopBlacklistDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public ShopBlacklistDTO getByCompanyName(String companyName, Integer type) {
        Wrapper<ShopBlacklistDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("type", type);//黑名单类型 0用户黑名单，1店铺黑名单
        wrapper.eq("company_name", companyName);

        return this.selectOne(wrapper);
    }

    @Override
    public ShopBlacklistDTO getByShopId(Long shopId, Integer type) {
        Wrapper<ShopBlacklistDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("type", type);//黑名单类型 0用户黑名单，1店铺黑名单
        wrapper.eq("shop_id", shopId);

        return this.selectOne(wrapper);
    }

    /**
     * 条件查询 黑名单
     * @param shopBlacklistBO
     * @return
     */
    @Override
    public Page<ShopBlacklistVO> getListBySelect(ShopBlacklistBO shopBlacklistBO) {
        Page<ShopBlacklistVO> page = new Page<>(shopBlacklistBO.getPageNo(), shopBlacklistBO.getPageSize());
        List<ShopBlacklistVO> shopBlacklistVOList = baseMapper.getListByType(page,shopBlacklistBO);
        page.setRecords(shopBlacklistVOList);
        return page;
    }

    @Override
    public boolean deleteBlackById(Long id) {
        ShopBlacklistDTO shopBlacklistDTO = this.selectById(id);
        /**恢复审核结果
         * 1.若是用户审核状态和用户状态改为打回。包括审核表，用户表，资质表
         * 2.若是点铺审核状态改为未提交过。包括店铺表
         * 黑名单类型 0用户注册黑名单，1用户开店黑名单
         */
        if (shopBlacklistDTO.getType().equals(0)){
            //修改审核表
            ShopInfoAuditDTO shopInfoAuditDTO = new ShopInfoAuditDTO();
            shopInfoAuditDTO.setId(shopBlacklistDTO.getShopInfoAuditId());
            shopInfoAuditDTO.setStatus(ShoppingCodeEnum.AUDIT_RETURM.getCode());//审核状态 0审核中，1审核通过，2审核打回，3审核不通过,4重新提交
            shopInfoAuditService.updateById(shopInfoAuditDTO);
            //修改用户信息表
            ShopInfoAuditDTO newShopInfoAuditDTO = shopInfoAuditService.selectById(shopBlacklistDTO.getShopInfoAuditId());
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            userInfoDTO.setId(newShopInfoAuditDTO.getUserId());
            userInfoDTO.setStatus(ShoppingCodeEnum.AUDIT_RETURM.getCode());
            //修改资质
            Wrapper<ShopQualificationAuditDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("shop_audit_id", shopBlacklistDTO.getShopInfoAuditId()).eq("audit_type", ShoppingCodeEnum.AUDIT_TYPE_LOGIN.getCode());
            List<ShopQualificationAuditDTO> dtos = shopQualificationAuditService.selectList(wrapper);
            for (ShopQualificationAuditDTO dto : dtos){
                ShopQualificationAuditDTO shopQualificationAuditDTO = new ShopQualificationAuditDTO();
                shopQualificationAuditDTO.setId(dto.getId());
                shopQualificationAuditDTO.setStatus(ShoppingCodeEnum.AUDIT_RETURM.getCode());
                shopQualificationAuditService.updateById(shopQualificationAuditDTO);
            }
        }else {
            ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
            shopInfoDTO.setId(shopBlacklistDTO.getShopId());
            shopInfoDTO.setSellerStatus(ShoppingCodeEnum.AUDIT_NULL.getCode());//开店审核状态 0创建、1审核通过、2审核打回、3，审核不通过。4重新提交。5.未提交
            iShopInfoService.updateById(shopInfoDTO);

            //删除开店审核表
            Wrapper<ShopInfoAuditDTO> awrapper = new EntityWrapper<>();
            awrapper.eq("shop_id", shopBlacklistDTO.getShopId());
            awrapper.eq("type", ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode());
            shopInfoAuditService.delete(awrapper);
            //删除开店审核相关资质
            Wrapper<ShopQualificationAuditDTO> qwrapper = new EntityWrapper<>();
            qwrapper.eq("shop_id", shopBlacklistDTO.getShopId());
            qwrapper.eq("audit_type", ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode());
            shopQualificationAuditService.delete(qwrapper);
            //删除开店审核相关类目
            Wrapper<ShopCategoryRelationAuditDTO> rwrapper = new EntityWrapper<>();
            rwrapper.eq("shop_id", shopBlacklistDTO.getShopId());
            rwrapper.eq("type", ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode());
            shopCategoryRelationAuditService.delete(rwrapper);
            //删除开店审核相关新增品牌
            Wrapper<ShopBrandRelationAuditDTO> bwrapper = new EntityWrapper<>();
            bwrapper.eq("shop_id", shopBlacklistDTO.getShopId());
            bwrapper.eq("type", ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode());
            shopBrandRelationAuditService.delete(bwrapper);

        }
        Wrapper<ShopBlacklistDTO> bwrapper = new EntityWrapper<>();
        bwrapper.eq("id", id);
        return this.delete(bwrapper);
    }

    @Override
    @Async
    public void deleteBlackBatchIds(String ids) {
        String[] arr = ids.split(",");
        for (String id : arr){
            deleteBlackById(Long.parseLong(id));
        }
    }

    @Override
    public boolean deleteBlackList(Long id) {
        ShopBlacklistDTO shopBlacklistDTO = this.selectById(id);
        /**恢复审核结果
         * 1.若是用户审核状态和用户状态改为打回。包括审核表，用户表，资质表
         * 2.若是点铺审核状态改为未提交过。包括店铺表
         * 黑名单类型 0用户注册黑名单，1用户开店黑名单
         */
        if (shopBlacklistDTO.getType().equals(0)){
            //修改审核表
            AuditDTO auditDTO = new AuditDTO();
            auditDTO.setId(shopBlacklistDTO.getShopInfoAuditId());
            auditDTO.setStatus(ShoppingCodeEnum.AUDIT_RETURM.getCode());//审核状态 0审核中，1审核通过，2审核打回，3审核不通过,4重新提交
            iAuditService.updateById(auditDTO);
            //修改用户信息表
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            userInfoDTO.setId(auditDTO.getUserId());
            userInfoDTO.setStatus(ShoppingCodeEnum.AUDIT_RETURM.getCode());
            iUserInfoService.updateById(userInfoDTO);
        }else {
            ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
            shopInfoDTO.setId(shopBlacklistDTO.getShopId());
            shopInfoDTO.setSellerStatus(ShoppingCodeEnum.AUDIT_NULL.getCode());//开店审核状态 0创建、1审核通过、2审核打回、3，审核不通过。4重新提交。5.未提交
            iShopInfoService.updateById(shopInfoDTO);
        }
        Wrapper<ShopBlacklistDTO> bwrapper = new EntityWrapper<>();
        bwrapper.eq("id", id);
        return this.delete(bwrapper);
    }

    @Override
    public void deleteBlackListBatchIds(String ids) {
        String[] arr = ids.split(",");
        for (String id : arr){
            deleteBlackList(Long.parseLong(id));
        }
    }
}
